package JZ3_从尾到头打印链表;

import java.util.ArrayList;
import java.util.Stack;

class ListNode{
    public int val;
    public ListNode next;
    ListNode(int val){
        this.val = val;
        this.next = null;
    }
}

public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<>();
        ListNode cur = listNode;

        while (cur != null){
            stack.push(cur.val);
            cur = cur.next;
        }

        ArrayList<Integer> list = new ArrayList<>();
        while (!stack.isEmpty()){
            list.add(stack.pop());
        }

        return list;

    }

    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        n1.next = n2;
        n2.next = n3;
        ListNode head = n1;
        Solution solution = new Solution();
        ArrayList<Integer> list = new ArrayList<>();
        list = solution.printListFromTailToHead(n1);
        for (Integer e : list){
            System.out.println(e);
        }

    }
}
